﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;


namespace EnterpriseLibraryGeneric.DataAccess
{
    public class Database
    {
        //Determina que componente de acceso se quiere utilizar Ej: Data Access Application Block, etc.
        private string componentDataProvider;

        //Determina el tipo de base de datos Ej: Sql Server, Oracle, etc.
        private string dataProvider;

        //Crea una instancia de DataAccessComponentManager
        private IDataAccessComponentManager dacManager;

        public Database()
        {
            //Obtiene las claves desde el Web.config
            this.componentDataProvider = ConfigurationManager.AppSettings.Get("ComponentDataProvider");
            this.dataProvider = ConfigurationManager.AppSettings.Get("DataProvider");

            this.dacManager = DataAccessComponentFactory.GetFactory(this.componentDataProvider, dataProvider);
        }

        /// <summary>
        /// Ejecuta instrucción o Stored procedure
        /// </summary>
        /// <param name="commandToExecute">Comando a Ejecutar</param>
        /// <param name="commandType">Tipo de comando Ej: Stored Procedure, Text, etc</param>
        /// <returns>La cantidad de registros afectados</returns>
        public int ExecuteNonQuery(string commandToExecute, CommandType commandType)
        {
            try
            {
                return dacManager.ExecuteNonQuery(commandToExecute, commandType);
            }
            catch (Exception ex)
            {
                throw new Exception (("Error ExecuteNonQuery"), ex);
            }
        }

        /// <summary>
        /// Ejecuta instrucción o Stored procedure
        /// </summary>
        /// <param name="commandToExecute">Comando a Ejecutar</param>
        /// <param name="commandType">Tipo de comando Ej: Stored Procedure, Text etc</param>
        /// <param name="parametros">Listado de parametros</param>
        /// <returns>La cantidad de registros afectados</returns>
        public int ExecuteNonQuery(string commandToExecute, CommandType commandType, IList<DataAccessParameter> parameters)
        {
            try
            {
                return dacManager.ExecuteNonQuery(commandToExecute, commandType, parameters);
            }
            catch (Exception ex)
            {
                throw new Exception(("Error ExecuteNonQuery con parametros"), ex);
            }
        }

        /// <summary>
        /// Ejecuta instrución o Stored procedure
        /// </summary>
        /// <param name="commandToExecute">Comando a Ejecutar</param>
        /// <param name="commandType">Tipo de comando Ej: Stored Procedure, Text etc</param>
        /// <returns>Dataset con el resultado de la consulta</returns>
        public DataSet ExecuteQuery(string commandToExecute, CommandType commandType)
        {
            try
            {
                return dacManager.ExecuteQuery(commandToExecute, commandType);
            }
            catch (Exception ex)
            {
                throw new Exception(("Error ExecuteQuery"), ex);
            }
        }

        /// <summary>
        /// Ejecuta instrución o Stored procedure
        /// </summary>
        /// <param name="commandToExecute">Comando a Ejecutar</param>
        /// <param name="commandType">Tipo de comando Ej: Stored Procedure, Text etc</param>
        /// <param name="parametros">Listado de parametros</param>
        /// <returns>Dataset con el resultado de la consulta</returns>
        public DataSet ExecuteQuery(string commandToExecute, CommandType commandType, IList<DataAccessParameter> parameters)
        {
            try
            {
                return dacManager.ExecuteQuery(commandToExecute, commandType, parameters);
            }
            catch (Exception ex)
            {
                throw new Exception(("Error ExecuteQuery con parametros"), ex);
            }
        }

        /// <summary>
        /// Inicia una transaccion
        /// </summary>
        public void BeginTransaction()
        {
            try
            {
                dacManager.BeginTransaction();
            }
            catch (Exception ex)
            {
                throw new Exception(("Error BeginTransaction"), ex);
            }

        }

        /// <summary>
        /// Anula una transaccion
        /// </summary>
        public void RollBackTransaction()
        {
            try
            {
                dacManager.RollBackTransaction();
            }
            catch (Exception ex)
            {
                throw new Exception(("Error RollBackTransaction"), ex);
            }
        }
    }
}
